var app = getApp();

function baifenSum(goodsPrice,baifen) {
  var that = this
  
  // var goods = list;
   var totalAmt = 0;
   let bi = '';
  if (baifen == '自定义' ){
         bi = '0';
     }else if(baifen == '加价5%'){
         bi = '0.05';
     }else if(baifen == '加价10%'){
         bi = '0.1';
     }else if(baifen == '加价20%'){
         bi = '0.2';
     }
  var a =   accMul(goodsPrice, bi);
  
      totalAmt = accAdd(goodsPrice, accMul(goodsPrice, bi));
    

 
  return totalAmt;

};
exports.baifenSum = baifenSum

function payAllSum(list,price) {
  var that = this
  var goods = list;
  var totalAmt = 0;
  if (!list ){
        return 0;
     }
    
     
       
  for(var i =0;i<goods.length;i++){//123
  // console.log(goods);
  // debugger
  if(goods[i]['recipientInfo'] != null){
         for(var j =0;j<goods[i]['recipientInfo'].length;j++){//123
         if(goods[i]['recipientInfo'][j]['groupState'] == null){
             totalAmt = accAdd(totalAmt, accMul(price, goods[i]['recipientInfo'][j]['skuNum'])) 
         }
         }
    
       }                       
  }
  // if(totalAmt == 0){
  //     totalAmt = price;
  // }
 
 
  return totalAmt;

};
exports.payAllSum = payAllSum




function accSub(arg1, arg2) { 
    var r1, r2, m, n; 
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 } 
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 } 
    m = Math.pow(10, Math.max(r1, r2)); 
    n = (r1 >= r2) ? r1 : r2; 
    return ((arg1 * m - arg2 * m) / m).toFixed(n); 
} 
exports.accSub = accSub

function allSum(data) {
  var that = this
  var goods = data;
  var totalAmt = 0;
  if (!data){
        return 0;
     }     
     // debugger
  for(var i =0;i<goods.length;i++){//123
          if(goods[i].isCheak == true){         
                totalAmt = accAdd(totalAmt, goods[i]['totalPrice'])        
       
              /* break; */
          }
  
  }

  return totalAmt;

};
exports.allSum = allSum

/**
 * 加法函数，用来得到精确的加法结果
 * @param {Object} arg1
 * @param {Object} arg2
 */
function accAdd(arg1, arg2) {
 
  var r1 = deal(arg1);
  var r2 = deal(arg2);
  var m = Math.pow(10, Math.max(r1, r2))
 var aa = Number( (arg1 * m + arg2 * m) / m).toFixed(2)
  
   return aa
}
exports.accAdd = accAdd

/**
 * 乘法函数，用来得到精确的乘法结果
 * @param {Object} arg1
 * @param {Object} arg2
 */
function accMul(arg1, arg2) {
  var m = 0;
  m += deal(arg1);
  m += deal(arg2);
 
  var r1 = Number(arg1.toString().replace(".", ""));
  var r2 = Number(arg2.toString().replace(".", ""));
  return (r1 * r2) / Math.pow(10, m)
}


/**
 * 求小数点后的数据长度
 */
function deal(arg) {
  var t = 0;
  try {
    t = arg.toString().split(".")[1].length
  } catch (e) { }
  return t;
}
exports.deal = deal

function cart_badge(goodNum){
  if (goodNum >0) {
    wx.setTabBarBadge({
      index: 2,
      text: goodNum + ''
    })
  } else {
    wx.removeTabBarBadge({
      index: 2,
    })
  }
  
}
module.exports.cart_badge = cart_badge
function paySum(data) {
  var that = this
  var goods = data;
  var totalAmt = 0;
  if (!data ){
        return 0;
     }
    
     
       
  for(var i =0;i<goods.length;i++){//123
     
       totalAmt = accAdd(totalAmt, accMul(goods[i]['packCost'], goods[i]['goodsNum']))                         
  }
 
 
  return totalAmt;

};
exports.paySum = paySum

function payGoodsSum(data) {
  var that = this
  var goods = data;
  var totalAmt = 0;
  if (!data ){
        return 0;
     }
    
  for(var i =0;i<goods.length;i++){//123
  let zhi;
  if(goods[i]['discountPrice'] == 0 ||goods[i]['discountPrice'] == null ){
     zhi = goods[i]['unitPrice'];
  }else{
      zhi = goods[i]['discountPrice'];
  }
      
    totalAmt = accAdd(totalAmt, accMul(zhi, goods[i]['goodsNum']))
           
          
      
      
        
  }
 
  
 
  return totalAmt;

};

exports.payGoodsSum = payGoodsSum

//当前日期加减天数,falg:true表示只要年月日
function mathChangeDate(dat,method,days,flag){
    
    
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y =date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
    date = Y + '-'  + M+ '-' + D;
    
  //method:'+' || '-'
  //ios不解析带'-'的日期格式，要转成'/'，不然Nan，切记
  var dateVal = date.replace(/-/g, '/');
  var timestamp = Date.parse(dateVal);
  if(method == '+'){
    timestamp = timestamp / 1000 + 24 * 60 * 60 * days;
  } else if (method == '-'){
    timestamp = timestamp / 1000 - 24 * 60 * 60 * days;
  }
  return toDate(timestamp,flag);
}
exports.mathChangeDate = mathChangeDate
function toDate(number,flag,part) {
  var n = number;
  var date = new Date(parseInt(n) * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  // var h = date.getHours();
  // h = h < 10 ? ('0' + h) : h;
  // var minute = date.getMinutes();
  // var second = date.getSeconds();
  // minute = minute < 10 ? ('0' + minute) : minute;
  // second = second < 10 ? ('0' + second) : second;
  if(flag){
      if(part == "year"){
          return y;
      }else if(part == "month"){
          return m;
      }else if(part == "date"){
          return n;
      }
      return y + '-' + m + '-' + d;
  }
  return y + '-' + m + '-' + d;
}
function formatSeconds() {
    // var time2 = new Date()
    // console.log(time2)        
    //获取当前时间  
    // var timestamp = Date.parse(new Date());  
    // 
    //     timestamp = timestamp / 1000;  
    //     var n = timestamp * 1000;  
        var date = new Date();  
        //年  
        var Y = date.getFullYear();  
        //月  
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);  
        //日  
        var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();  
        //时  
        var hh = date.getHours();  
        //分  
        var mm = date.getMinutes();  
        //秒  
        var ss= date.getSeconds();  
        
        var a =  Y+"/"+M+"/"+D+" "+hh+":"+mm+":"+ss;
        var b = Y+"/"+M+"/"+D+" "+'23'+":"+'59'+":"+'59';
        
    // var d1 = new Date(a);
    // var d2 = new Date(b);
     var d1 = Date.parse(new Date(a));
        var d2 =Date.parse(new Date(b));
   let result =  parseInt(d2-d1)
 　　return result
}
exports.formatSeconds = formatSeconds


    
  
  
 function js_date_time(number,format) { //转换日期
              var formateArr  = ['Y','M','D','h','m','s'];
              var returnArr   = [];
              var date = new Date(number);
              returnArr.push(date.getFullYear());
              returnArr.push(formatNumber(date.getMonth() + 1));
              returnArr.push(formatNumber(date.getDate()));
              returnArr.push(formatNumber(date.getHours()));
              returnArr.push(formatNumber(date.getMinutes()));
              returnArr.push(formatNumber(date.getSeconds()));
              for (var i in returnArr)
              {
                  format = format.replace(formateArr[i], returnArr[i]);
              }
              return format;
          
          
}
 function  formatNumber(n) {
            n = n.toString();
            return n[1] ? n : '0' + n
  }

exports.js_date_time = js_date_time

